/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import LibreriaServer.LServer;
import MailCliente.MailCliente;
import java.util.logging.Level;
import java.util.logging.Logger;
import mailserver.MailServer1;
import mailserver.Server;

/**
 *
 * @author Marisol
 */
public class IUMailServidor extends javax.swing.JFrame {

    private MailServer1 mailservidor;

    /**
     * Creates new form IUMailServidor
     */
    public IUMailServidor() {
        initComponents();
        Server servidor = new Server();


        LServer.mostrarCombox(ListaMailServer, servidor.getListaServer());


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        nameuser = new javax.swing.JTextField();
        crearCuenta = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TablaUsuarios = new javax.swing.JTable();
        ListaMailServer = new javax.swing.JComboBox();
        mostrarmsj = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Nombre");

        crearCuenta.setText("Crear Cuenta");
        crearCuenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearCuentaActionPerformed(evt);
            }
        });

        TablaUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Usuario"
            }
        ));
        jScrollPane2.setViewportView(TablaUsuarios);

        ListaMailServer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ListaMailServerMouseClicked(evt);
            }
        });
        ListaMailServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ListaMailServerActionPerformed(evt);
            }
        });

        mostrarmsj.setText("mostrar Mensajes");
        mostrarmsj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mostrarmsjActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(ListaMailServer, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(41, 41, 41)
                                    .addComponent(jLabel2)
                                    .addGap(34, 34, 34)
                                    .addComponent(nameuser, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(132, 132, 132)
                                    .addComponent(crearCuenta))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(136, 136, 136)
                .addComponent(mostrarmsj)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(ListaMailServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nameuser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(crearCuenta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(mostrarmsj)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void crearCuentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearCuentaActionPerformed
        try {
            // TODO add your handling code here:

            String usuario = nameuser.getText();
            mailservidor = new MailServer1(ListaMailServer.getSelectedItem().toString());
//            mailservidor.recuperar();
//            System.out.println(mailservidor.toString());
              int estado = mailservidor.anadirClientes(usuario+ListaMailServer.getSelectedItem().toString());
              
            if (estado == 0) {
                MailCliente cliente = new MailCliente(usuario+ListaMailServer.getSelectedItem().toString(), mailservidor);
                cliente.guardar();
                
                LServer.mostrarLista(TablaUsuarios, mailservidor.getListaUsuarios());
            }
            nameuser.setText("");
        } catch (Exception ex) {
            Logger.getLogger(IUMailServidor.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_crearCuentaActionPerformed

    private void ListaMailServerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListaMailServerMouseClicked
 
    }//GEN-LAST:event_ListaMailServerMouseClicked

    private void mostrarmsjActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mostrarmsjActionPerformed
        // TODO add your handling code here:s
        System.out.println(LServer.Imprimir(mailservidor.getListaMensajes()));
        
    }//GEN-LAST:event_mostrarmsjActionPerformed

    private void ListaMailServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ListaMailServerActionPerformed
        // TODO add your handling code here:
        
               try {
            // TODO add your handling code here:
            
              mailservidor = new MailServer1(ListaMailServer.getSelectedItem().toString());
            LServer.mostrarLista(TablaUsuarios, mailservidor.getListaUsuarios());
        } catch (Exception ex) {
            Logger.getLogger(IUMailServidor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ListaMailServerActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IUMailServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IUMailServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IUMailServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IUMailServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IUMailServidor().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ListaMailServer;
    private javax.swing.JTable TablaUsuarios;
    private javax.swing.JButton crearCuenta;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton mostrarmsj;
    private javax.swing.JTextField nameuser;
    // End of variables declaration//GEN-END:variables
}
